<?php
/**
 * Login.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/05
 * Time: 21:47
 */
namespace app\api\open\user;

use app\api\open\Base;
use think\Validate;

class Login extends Base
{
    protected $username = "";
    protected $password = "";

    /**
     * Login constructor.
     * @param array $param
     * @throws \Exception
     */
    public function __construct($param=[])
    {
        $this->prevent();

        parent::__construct($param);

        $param = $this->params;

        $validate = new Validate([
            "username" => "require",
            "password" => "require|min:6"
        ],[
            "username.require" => "用户名必填",
            "password.require" => "密码必填",
            "password.min" => "密码至少6位以上"
        ]);

        if($validate->check($param) === false) {
            throw new \Exception($validate->getError(),1);
        }

        $this->username = $param['username'];
        $this->password = $param['password'];
    }

    /**
     * @param array $param
     * @return array|false|null|\PDOStatement|string|\think\Model
     * @throws \Exception
     */
    public function signIn($param=[])
    {
        return \app\common\simple_rbac\User::getInstance()->auth($this->username,$this->password);
    }
}